<template>
    <div>
        <sticky-top>
            <div class="title">
              <span>{{ isCreate ? '添加文档资料' : '修改文档资料' }}</span>
              <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
              <el-divider></el-divider>
            </div>
        </sticky-top>
        <div class="container">
            <div class="wrap">
              <el-row>
                <el-col :lg="16" :md="20" :sm="24" :xs="24">
                  <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
                    <el-form-item label="标题" prop="title">
                      <el-input size="medium" v-model="form.title" placeholder="请填写标题"></el-input>
                    </el-form-item>
                    <el-form-item label="内容详情" prop="content">
                        <template>
                            <tinymce :defaultContent="form.content" @change="change"  upload_url="/cms/file" />
                          </template>
                    </el-form-item>
                    <el-form-item class="submit">
                      <el-button
                        v-permission="{ permission: ['添加文档自来', '修改文档资料'], type: 'disabled' }"
                        type="primary"
                        @click="submitForm('form')"
                        >保 存</el-button
                      >
                      <el-button @click="resetForm('form')">重 置</el-button>
                    </el-form-item>
                  </el-form>
                </el-col>
              </el-row>
            </div>
        </div>
    </div>
    
</template>
<script>

import Tinymce from '@/component/base/tinymce'
import DocumentModel from '@/model/document'

    export default {
        components: {
            Tinymce,
        },
        data() {
            return {
                form: {
                    title: '',
                    content: '',
                }
            }
        },
        props: {
            isCreate: {
                type: Boolean,
                default: true
            },
            documentId: {
                type: Number,
                default: 0
            }
        },
        async mounted() {
            if (!this.isCreate) {
                const res = await DocumentModel.get(this.documentId)
                this.form = res
                // this.getDocument()
                console.log(this.form)
            }
        },
        methods: {
            async getDocument() {
                const res = await DocumentModel.get(this.documentId)
                this.form = res
            } ,
            async submitForm() {
                const form = { ...this.form }
                console.log(form)
                let res
                if (this.isCreate) {
                    res = await DocumentModel.create(form)
                } else {
                    res = await DocumentModel.update(this.documentId, form)
                }
                if (res.code < window.MAX_SUCCESS_CODE) {
                    this.$message.success(`${res.message}`)
                    this.$emit('dialogClose')
                }
            },
            resetForm(form) {
                this.$refs[form].resetFields()
            },
            change(val) {
                this.form.content = val
            },
            back() {
                console.log(123123)
                this.$emit('dialogClose')
            }
        },
    }
</script>
<style lang="scss" scoped>
    @import '@/assets/style/create-edit';
</style>